<script setup lang="ts">
import ConsultItem from './ConsultItem.vue'
import type { ConsultType } from '@/enums'
import { ref } from 'vue'
import { getConsultOrderList } from '@/api/consult'
import type { ConsultOrderItem } from '@/types/consult'

// 接收问诊类型
const props = defineProps<{
  type: ConsultType // 问诊类型：1找医生 2== 极速问诊 == 3开药问诊
}>()

const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)

// 列表请求的分页参数
const params = {
  type: props.type, // 问诊类型
  current: 1, // 第几页
  pageSize: 10 // 每页几条数据
}

const onLoad = async () => {
  // 异步更新数据
  // setTimeout 仅做示例，真实场景中一般为 ajax 请求
  // setTimeout(() => {
  //   for (let i = 0; i < 10; i++) {
  //     list.value.push(list.value.length + 1)
  //   }
  const { rows, total } = await getConsultOrderList(params)
  list.value.push(...rows)
  // 加载状态结束
  loading.value = false

  // 数据全部加载完成
  if (list.value.length === total) {
    // 没有数据了
    finished.value = true
  } else {
    // 还有数据，页码+1
    params.current++
  }
  // }, 1000)
}

const onDelete = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item @on-delete="onDelete" v-for="item in list" :item="item" :key="item.id" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
